package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.book.mapper.BookMapper;
import com.itany.book.pojo.Book;
import com.itany.book.pojo.Supplier;
import com.itany.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/25 10:15
 * Describe:
 * version:1.0
 */

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public Book findById(Integer id) {

        return bookMapper.selectById(id);

    }

    @Override
    public PageInfo<Book> findAll(Integer page,
                                  Integer rows,
                                  String bookname,
                                  String isbn,
                                  Integer pressid,
                                  Integer supplierid) {

        PageHelper.startPage(page, rows);
        List<Book> books = bookMapper.selectAll(bookname,isbn,pressid,supplierid);
        for (Book b: books){
            if (b.getSuppliers() != null){
                for (Supplier s: b.getSuppliers()){
                    System.out.println("|||||||||||||||||||");
                    System.out.println(s);
                }

            }

        }
        return new PageInfo<>(books);
    }
}
